//
//  WidgetScene.cpp
//  SimpleGame
//
//  Created by Scar1et on 2017/7/25.
//
//

// 头文件 -- 类库声明请放入头文件
#include "WidgetScene.h"

// 层级：影响显示与事件响应
enum {
    // 背景
    levelBGSimple = 1,
    // 内容
    levelContentSimple = 101,
    // 对话框
    levelDialogSimple = 1001
};

// TAG：视图标记
enum {
    tagBack = 1,
    tagLabel = 2,
    tagMenu = 3
};

// 创建场景
Scene* WidgetScene::createScene() {
    Scene* sWidget = Scene::create();
    sWidget->addChild(WidgetScene::create());
    return sWidget;
}

// 初始化场景，自动调用
bool WidgetScene::init() {
    // 先调用父类Init
    if (!Layer::init()) {
        return false;
    }
    
    // 手机按键监听器
    listenerKeyboard = EventListenerKeyboard::create();
    listenerKeyboard->onKeyReleased = CC_CALLBACK_2(WidgetScene::onKeyReleased, this);
    // 添加到事件分发器中
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerKeyboard, this);
    
    // 开始初始化
    // 成员变量初始化
    // 可视区域尺寸
    sizeVisible = Director::getInstance()->getVisibleSize();
    // 可视区域的出发点坐标
    positionOrigin = Director::getInstance()->getVisibleOrigin();
    // 屏幕尺寸
    sizeWin = Director::getInstance()->getWinSize();
    
    this->initContent();
    
    // 初始化成功
    return true;
}

// 内容初始化
void WidgetScene::initContent() {
    // 返回按钮
    auto lBack = Label::createWithSystemFont("返回", "arial.ttf", 28);
    lBack->setTextColor(Color4B::WHITE);
    lBack->setHorizontalAlignment(TextHAlignment::LEFT);
    lBack->setVerticalAlignment(TextVAlignment::CENTER);
    lBack->setDimensions(120, 40);
    auto milBack = MenuItemLabel::create(lBack, CC_CALLBACK_1(WidgetScene::onClicked, this));
    milBack->setTag(tagBack);
    milBack->setPosition(Vec2(positionOrigin.x + 80, positionOrigin.y + sizeVisible.height - 40));
    
    // Label按钮
    auto lLabel = Label::createWithSystemFont("Label", "arial.ttf", 28);
    lLabel->setTextColor(Color4B::WHITE);
    lLabel->setHorizontalAlignment(TextHAlignment::LEFT);
    lLabel->setVerticalAlignment(TextVAlignment::CENTER);
    lLabel->setDimensions(120, 40);
    auto milLabel = MenuItemLabel::create(lLabel, CC_CALLBACK_1(WidgetScene::onClicked, this));
    milLabel->setTag(tagLabel);
    milLabel->setPosition(Vec2(positionOrigin.x + 80, positionOrigin.y + sizeVisible.height - 100));
    
    // Menu按钮
    auto lMenu = Label::createWithSystemFont("Menu", "arial.ttf", 28);
    lMenu->setTextColor(Color4B::WHITE);
    lMenu->setHorizontalAlignment(TextHAlignment::LEFT);
    lMenu->setVerticalAlignment(TextVAlignment::CENTER);
    lMenu->setDimensions(120, 40);
    auto milMenu = MenuItemLabel::create(lMenu, CC_CALLBACK_1(WidgetScene::onClicked, this));
    milMenu->setTag(tagMenu);
    milMenu->setPosition(Vec2(positionOrigin.x + 220, positionOrigin.y + sizeVisible.height - 100));
    
    // 按钮菜单 -- Widget【可添加多个，用NULL结尾】
    auto mWidget = Menu::create(milBack, milLabel, milMenu, NULL);
    mWidget->setPosition(Vec2::ZERO);
    this->addChild(mWidget, levelContentSimple);
}

// 返回功能
void WidgetScene::back() {
    // 从事件分发器移除手机按键监听器
    Director::getInstance()->getEventDispatcher()->removeEventListener(listenerKeyboard);
    // 从场景堆栈中pop出一个场景，替换现在正运行的场景，而运行中的场景将被删除
    Director::getInstance()->popScene();
}

// 点击监听
void WidgetScene::onClicked(Ref* rSender) {
    int tag = ((MenuItem*)rSender)->getTag();
    switch (tag) {
        case tagBack:
            // 返回按钮被点击
            this->back();
            break;
        case tagLabel:
            // Label按钮被点击
            Director::getInstance()->pushScene(LabelScene::createScene());
            break;
        case tagMenu:
            // Menu按钮被点击
            Director::getInstance()->pushScene(MenuScene::createScene());
            break;
        default:
            break;
    }
}

// 键盘点击监听
void WidgetScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) {
    switch (keyCode){
        case EventKeyboard::KeyCode::KEY_BACK:
            // 返回键
            this->back();
            break;
        default:
            break;
    }
}
